package com.zdlog.wenjuan.controller.web;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dataobject.MainProblem;
import com.zdlog.wenjuan.service.MainProblemService;
import com.zdlog.wenjuan.util.KeyUtil;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.controller
 * @author: Kuaile
 * @create: 2018-02-06 17:21
 * 主分支
 **/

@Controller
@RequestMapping("/mainProblem")
@ResponseBody
@Slf4j
public class MainProblemController {


    @Autowired
    private MainProblemService service;



    @GetMapping("/add")
    public ModelAndView add(@RequestParam(value = "formid") String formid, Map<String, Object>map) {
        map.put("id", formid);
        return new ModelAndView("mainProblem/main_problem_add");
    }


    @PostMapping("/toadd")
    public ResultBoolenVO toAdd(@RequestParam(value = "formid") String formid,
                                @RequestParam(value = "problem") String name) {
        MainProblem mainProblem = new MainProblem();
        mainProblem.setFormId(formid);
        mainProblem.setMainProblemId(KeyUtil.genUniqueKey());
        mainProblem.setMainProblemName(name);
        MainProblem result = service.save(mainProblem);
        if (result != null)
            return ResultBoolenVOUtil.success();
        return ResultBoolenVOUtil.error("添加失败");
    }

    @PostMapping("/doDelete")
    public ResultBoolenVO doDelete(@RequestParam(value = "id") String id) {
        //TODO 删除主分支
        return service.deleteMainProblemAndOption(id);
    }

}
